package formServlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
 
/////////////////////////////////////////////////////////////////////////
// This servlet is called when the user enters a username and password
// from the sign in field.  
// This servlet checks whether the username and password exists in 
// the database and redirects the user to a successful or unsuccessful 
// sign in page.
/////////////////////////////////////////////////////////////////////////

public class FrontpageSignin extends HttpServlet {
 
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		getServletContext().getRequestDispatcher("SNSP.html").forward(req, resp);
	}
 
	@SuppressWarnings("unused")
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		/////////////////////////////////////////////////////////////////////////////
		// request.getParameters looks up the strings from the form with names
		// username and password
		/////////////////////////////////////////////////////////////////////////////
		
		String userName = request.getParameter("username");
		String password = request.getParameter("password");
		System.out.println("You Have Entered username: " + userName + " password " + password);
		
		// DatastoreServiceFactory is our database in app engine
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		
		////////////////////////////////////////////////////////////////////////////////
		// The try portion of the code creates a key with the userName string,
		// gets the password associated with the userName,
		// checks to see if the password entered in the form matches
		// the password in the database
		// 
		// If the password matches, a cookie is created and passed to the 
		// successful sign in page.
		// otherwise the user is redirected to the unsuccessful sign in page
		////////////////////////////////////////////////////////////////////////////////
		
		
		try {
			
			Key key = KeyFactory.createKey("UserSignUp", userName);
			Entity e=datastore.get(key);
//			System.out.print(e.getProperties());
			String pwd=e.getProperty("Password").toString();
//			System.out.println("The Password is " + pwd);
			
			// Case 1: user name and password both exist.
			if(pwd.equalsIgnoreCase(password))
			{
		        Cookie c = new Cookie("passUsername", userName);
		         response.addCookie(c); 
				response.sendRedirect("SignInPage.html");
			}
			
			// Case 2: user name exists, but password incorrect
			
			else
			{
				Cookie userExist;	
				userExist = new Cookie ("userExist", "true");
				
			 	response.addCookie(userExist);
			 	
				Cookie c = new Cookie ("passUsername", userName);	
				response.addCookie(c);
				response.sendRedirect("UnsuccessfulSignIn.html");
			}
			
		} // try
		
		// Case 3: username does not exists
		catch (EntityNotFoundException e) {
			// TODO Auto-generated catch block
			Cookie userExist;
			userExist = new Cookie ("userExist", "false");
			response.addCookie(userExist);
			Cookie c = new Cookie ("passUsername", userName);	
			response.addCookie(c);
			response.sendRedirect("UnsuccessfulSignIn.html");
		}
		
		
//		response.sendRedirect("SignInPage.html");
	}
 

 
}